package com.beiding.dao.impl;

import com.beiding.dao.ResourceOperations;
import com.beiding.pojo.order.Order;
import com.google.common.collect.Lists;
import org.bson.Document;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Repository
public class OrderDaoImpl implements ResourceOperations<Order> {

    private MongoTemplate mongoTemplate;

    public OrderDaoImpl(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

    @Override
    public Class<Order> type() {
        return Order.class;
    }

    @Override
    public MongoOperations mongoOperations() {
        return mongoTemplate;
    }


    private Document fs() {

        Document document = new Document();

        //将所有的时间戳全部隐藏
        document.put("createTimestamp", false);
        document.put("paidTimestamp", false);
        document.put("acceptedTimestamp", false);
        document.put("useTimestamp", false);
        document.put("expireTimestamp", false);

        //隐藏敏感数据
        document.put("checkInKey", false);
        document.put("checkInKeyErrorTime", false);
        document.put("platformCostRate", false);

        return document;
    }


    public Order getInsensitive(String id){
        return mongoOperations().findOne(new BasicQuery(new Document("_id", id), fs()), Order.class);
    }



    public List<Order> getNonPayment(String name) {

        Document q = new Document();
        q.put("tenant", name);
        q.put("state", Order.CREATE);
        return Optional.of(mongoOperations().find(new BasicQuery(q, fs()), Order.class)).get();

    }



    public List<Order> getPayed(String name) {
        Document q = new Document();
        q.put("tenant", name);


        List<Document> list = new ArrayList<>();

        //付过款的订单有

        //等待受理的
        list.add(new Document("state", Order.PAID));

        //等待入住的
        list.add(new Document("state", Order.ACCEPTED));

        //申请退款的
        list.add(new Document("state", Order.REFUND));

        //申请退款差生分歧的
        list.add(new Document("state", Order.REFUND_DISPUTE));

        //退款仲裁
        list.add(new Document("state", Order.REFUND_ARBITRATION));

        //正在使用中的
        list.add(new Document("state", Order.USING));

        //锁死
        list.add(new Document("state", Order.LOCK));

        //退押金
        list.add(new Document("state", Order.REFUND_DEPOSIT));

        //房东要求扣留押金的
        list.add(new Document("state", Order.WITHHOLD_DEPOSIT));

        //扣留押金产生分歧并且仲裁
        list.add(new Document("state", Order.WITHHOLD_DEPOSIT_ARBITRATION));

         //两个条件的并条件
        q.put("$or", list);

        return Optional.of(mongoOperations().find(new BasicQuery(q, fs()), Order.class)).get();

    }

    public List<Order> getEvaluate(String name) {
        Document q = new Document();
        q.put("tenant", name);
        q.put("state", Order.EVALUATE);
        return Optional.of(mongoOperations().find(new BasicQuery(q, fs()), Order.class)).get();

    }



    public List<Order> getToDoOrder(String name) {

        Document q = new Document();
        q.put("landlord", name);


        Lists.newArrayList(new Document("state", Order.LOCK), new Document("state", Order.ACCEPTED), new Document("state", Order.PAID), new Document("state", Order.REFUND));

        List<Document> list = new ArrayList<>();

        //       需要房东处理的订单有:

        //付过款
        list.add(new Document("state", Order.PAID));

        //受理后
        list.add(new Document("state", Order.ACCEPTED));

        //申请退款
        list.add(new Document("state", Order.REFUND));

        //退款仲裁
        list.add(new Document("state", Order.REFUND_ARBITRATION));

        //锁死
        list.add(new Document("state", Order.LOCK));

        //退押金
        list.add(new Document("state", Order.REFUND_DEPOSIT));

        //扣留押金而产生分歧  房东可以提起仲裁
        list.add(new Document("state", Order.WITHHOLD_DEPOSIT_DISPUTE));

        //扣留押金产生分歧并且仲裁
        list.add(new Document("state", Order.WITHHOLD_DEPOSIT_ARBITRATION));

        q.put("$or", list);

        return Optional.of(mongoOperations().find(new BasicQuery(q, fs()), Order.class)).get();


    }


}
